package com.yuncheng.oaApi.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yuncheng.common.base.service.impl.BaseServiceImpl;
import com.yuncheng.oaApi.OaExcel.Utils.ExcelMergeCustomerCellHandler;
import com.yuncheng.oaApi.OaExcel.Utils.PoiUtils;
import com.yuncheng.oaApi.entity.NkYssbhjjxzb;
import com.yuncheng.oaApi.mapper.NkYssbhjjxzbMapper;
import com.yuncheng.oaApi.service.INkYssbhjjxzbService;
import com.yuncheng.oaApi.vo.YssbJxzbExcelVo;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;


import java.util.*;

@Service
public class NkYssbhjjxzbServiceImpl extends BaseServiceImpl<NkYssbhjjxzbMapper, NkYssbhjjxzb> implements INkYssbhjjxzbService {



    @Override
    public void exportRealTimeInventory( HttpServletResponse response,String jhid,String flx) {
        try {
            List<NkYssbhjjxzb> list = baseMapper.selectList(new LambdaQueryWrapper<NkYssbhjjxzb>()
                    .eq(NkYssbhjjxzb::getFjhid,jhid)
                    .eq(NkYssbhjjxzb::getFlx,flx));
            if (CollectionUtils.isEmpty(Collections.singleton(list))) {
                return;
            }
            List<YssbJxzbExcelVo> excelList = new ArrayList<>();
            int inNumTotal = 0, outNumTotal = 0, numTotal = 0;
            //遍历结果，进行处理
            for (NkYssbhjjxzb dto : list) {
                YssbJxzbExcelVo inventoryExcel = new YssbJxzbExcelVo();

                BeanUtils.copyProperties(inventoryExcel,dto);


                excelList.add(inventoryExcel);

            }
//            将结果排序,使相同科室名称的数据在一起，为后面行合并提供条件
//            Collator collator = Collator.getInstance(Locale.CHINA);
            excelList.removeIf(Objects::isNull);
//            excelList.sort(Comparator.comparing(YssbJxzbExcelVo::getFks, (s1, s2) -> collator.compare(s2, s1)));
            excelList.sort(Comparator.comparing(YssbJxzbExcelVo::getFks)
//                    .thenComparing(YssbJxzbExcelVo::getCreateTime)
                    .thenComparing(YssbJxzbExcelVo::getFjxsblx)
                    .thenComparing(YssbJxzbExcelVo::getFyjzblx));

            //将处理好的结果导出
            PoiUtils.exportExcel(response, "绩效指标", "绩效指标合计", excelList, YssbJxzbExcelVo.class, new ExcelMergeCustomerCellHandler(new int[]{0,1,2,3}, 0));
        } catch (Exception e) {
            throw new RuntimeException("绩效指标导出失败", e);
        }
    }




}
